//
//  YFGuideView.swift
//  微博
//
//  Created by 余仿 on 16/4/6.
//  Copyright © 2016年 YF. All rights reserved.
//

import UIKit

import SnapKit

@objc protocol YFGuideViewDelegate:NSObjectProtocol{

optional    func logInBtnDidClick()

optional    func registerBtnDidClick()

}

class YFGuideView: UIView {


    var delegate:YFGuideViewDelegate?


    //MARK:对外暴露的设置引导界面的借口
    func setupUI(imageName:String,desc:String,isHome:Bool = false){

        if isHome{

            startAnimation()

        }else{

            bigHouse.hidden = true

            bringSubviewToFront(circleIcon)
        }

        circleIcon.image = UIImage(named: imageName)

        tipLabel.text = desc

    }

    //MARK:圆圈懒加载
    lazy var circleIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))

    //MARK:大房子懒加载
    lazy var bigHouse:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

    lazy var backgroundView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))


    //MARK:文字标签懒加载
    lazy var tipLabel:UILabel = {

        let d = UILabel()

        d.numberOfLines = 0

        d.textAlignment = .Center

        return d

    }()

    //MARK:登录按钮懒加载
    lazy var logIn:UIButton = {

        let logIn = UIButton()

        logIn.setTitle("登录", forState: .Normal)

        logIn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

        logIn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)

        logIn .addTarget(self, action: "logInBtnDidClick", forControlEvents: .TouchUpInside)

        return logIn

    }()

    func logInBtnDidClick(){
        print("guideView  登录")

        delegate?.logInBtnDidClick?()
    }

    //MARK:注册按钮懒加载
    lazy var register:UIButton = {

        let register = UIButton()

        register.setTitle("注册", forState: .Normal)

        register.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)

        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)

        register .addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)

        return register

    }()


    func registerBtnDidClick(){
        print("guideView  注册")
        delegate?.registerBtnDidClick?()
    }

    //MARK:系统的构造方法
    override init(frame: CGRect) {

        super.init(frame: frame)

        addSubview(circleIcon)

        addSubview(backgroundView)

        addSubview(bigHouse)

        addSubview(tipLabel)

        addSubview(logIn)

        addSubview(register)

        let value:CGFloat = 237 / 255.0

        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK:设置圆圈的动画
    func startAnimation(){

        let basic = CABasicAnimation(keyPath: "transform.rotation")

        basic.repeatCount = MAXFLOAT

        basic.toValue = 2 * (M_PI)


        basic.duration = 10

        basic.removedOnCompletion = false

        circleIcon.layer.addAnimation(basic, forKey: nil)
    }

    //MARK:布局子控件
    override func layoutSubviews() {

        //MARK:布局大房子
        bigHouse.snp_makeConstraints { (make) -> Void in

            make.centerX.equalTo(self.centerXAnchor)

            make.centerY.equalTo(self.centerYAnchor).offset(-80)

        }
        //MARK:布局圆圈
        circleIcon.snp_makeConstraints { (make) -> Void in

            make.center.equalTo(bigHouse)
        }


        //MARK:布局文字标签
        tipLabel.snp_makeConstraints { (make) -> Void in

            make.centerX.equalTo(self)

            make.top.equalTo(circleIcon.snp_bottom).offset(20)

            make.width.equalTo(200)

            make.height.equalTo(50)

        }

        //MARK:布局登录按钮
        logIn.snp_makeConstraints { (make) -> Void in

            make.leading.equalTo(tipLabel.snp_leading)

            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            
            make.width.equalTo(80)
            
        }
        //MARK:布局注册按钮
        register.snp_makeConstraints { (make) -> Void in
            
            make.trailing.equalTo(tipLabel.snp_trailing)
            
            make.top.equalTo(tipLabel.snp_bottom).offset(20)
            
            make.width.equalTo(80)
            
        }
        
        //MARK:布局遮罩
        backgroundView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon)
        }
        
    }
    
}
